{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>x3</th>\n",
       "      <th>x4</th>\n",
       "      <th>x5</th>\n",
       "      <th>x6</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>93.18</td>\n",
       "      <td>21391758</td>\n",
       "      <td>7980207</td>\n",
       "      <td>6661555</td>\n",
       "      <td>0.373051</td>\n",
       "      <td>0</td>\n",
       "      <td>288972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>115.60</td>\n",
       "      <td>24927434</td>\n",
       "      <td>8779835</td>\n",
       "      <td>7839516</td>\n",
       "      <td>0.352216</td>\n",
       "      <td>0</td>\n",
       "      <td>350495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>114.13</td>\n",
       "      <td>28416511</td>\n",
       "      <td>9554676</td>\n",
       "      <td>8803979</td>\n",
       "      <td>0.336237</td>\n",
       "      <td>0</td>\n",
       "      <td>443213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>141.49</td>\n",
       "      <td>32039616</td>\n",
       "      <td>10509450</td>\n",
       "      <td>9733195</td>\n",
       "      <td>0.328014</td>\n",
       "      <td>0</td>\n",
       "      <td>526377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>180.52</td>\n",
       "      <td>37586166</td>\n",
       "      <td>13141254</td>\n",
       "      <td>11833760</td>\n",
       "      <td>0.349630</td>\n",
       "      <td>0</td>\n",
       "      <td>581898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>233.14</td>\n",
       "      <td>44505503</td>\n",
       "      <td>15941538</td>\n",
       "      <td>14030973</td>\n",
       "      <td>0.358193</td>\n",
       "      <td>4636933</td>\n",
       "      <td>528365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>268.07</td>\n",
       "      <td>51542283</td>\n",
       "      <td>18439550</td>\n",
       "      <td>16171817</td>\n",
       "      <td>0.357756</td>\n",
       "      <td>5574275</td>\n",
       "      <td>816119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>313.85</td>\n",
       "      <td>60818614</td>\n",
       "      <td>22270093</td>\n",
       "      <td>18921556</td>\n",
       "      <td>0.366172</td>\n",
       "      <td>5907373</td>\n",
       "      <td>967265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>355.91</td>\n",
       "      <td>71403223</td>\n",
       "      <td>26029310</td>\n",
       "      <td>22841850</td>\n",
       "      <td>0.364540</td>\n",
       "      <td>6875421</td>\n",
       "      <td>1115007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>389.47</td>\n",
       "      <td>82873816</td>\n",
       "      <td>29724781</td>\n",
       "      <td>27953721</td>\n",
       "      <td>0.358675</td>\n",
       "      <td>9328615</td>\n",
       "      <td>1287226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>392.82</td>\n",
       "      <td>91382135</td>\n",
       "      <td>31173422</td>\n",
       "      <td>31565720</td>\n",
       "      <td>0.341133</td>\n",
       "      <td>11237237</td>\n",
       "      <td>1375085</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>553.89</td>\n",
       "      <td>107482828</td>\n",
       "      <td>36449611</td>\n",
       "      <td>38835933</td>\n",
       "      <td>0.339120</td>\n",
       "      <td>13541607</td>\n",
       "      <td>1594182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>596.94</td>\n",
       "      <td>124234390</td>\n",
       "      <td>41405926</td>\n",
       "      <td>45444614</td>\n",
       "      <td>0.333289</td>\n",
       "      <td>15947698</td>\n",
       "      <td>1573830</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>582.52</td>\n",
       "      <td>135512072</td>\n",
       "      <td>42641557</td>\n",
       "      <td>51685711</td>\n",
       "      <td>0.314670</td>\n",
       "      <td>3837376</td>\n",
       "      <td>1758311</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>560.89</td>\n",
       "      <td>154201434</td>\n",
       "      <td>47548175</td>\n",
       "      <td>59858717</td>\n",
       "      <td>0.308351</td>\n",
       "      <td>4168317</td>\n",
       "      <td>2216017</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        x1         x2        x3        x4        x5        x6        y\n",
       "0    93.18   21391758   7980207   6661555  0.373051         0   288972\n",
       "1   115.60   24927434   8779835   7839516  0.352216         0   350495\n",
       "2   114.13   28416511   9554676   8803979  0.336237         0   443213\n",
       "3   141.49   32039616  10509450   9733195  0.328014         0   526377\n",
       "4   180.52   37586166  13141254  11833760  0.349630         0   581898\n",
       "5   233.14   44505503  15941538  14030973  0.358193   4636933   528365\n",
       "6   268.07   51542283  18439550  16171817  0.357756   5574275   816119\n",
       "7   313.85   60818614  22270093  18921556  0.366172   5907373   967265\n",
       "8   355.91   71403223  26029310  22841850  0.364540   6875421  1115007\n",
       "9   389.47   82873816  29724781  27953721  0.358675   9328615  1287226\n",
       "10  392.82   91382135  31173422  31565720  0.341133  11237237  1375085\n",
       "11  553.89  107482828  36449611  38835933  0.339120  13541607  1594182\n",
       "12  596.94  124234390  41405926  45444614  0.333289  15947698  1573830\n",
       "13  582.52  135512072  42641557  51685711  0.314670   3837376  1758311\n",
       "14  560.89  154201434  47548175  59858717  0.308351   4168317  2216017"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#-*- coding:utf-8 -*-\n",
    "# 2_2 增值税预测模型\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "inputfile1 = 'data2.csv'\n",
    "data = pd.read_csv(inputfile1)\n",
    "data # 1994到2013年间的各个影响因素的数据\n",
    "# 本小节中书中使用的是AdaptiveLasso，但是没有找到该函数，所以采用了其他变量选择模型\n",
    "# 书中给出的选出的变量为'x1', 'x3', 'x5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# # 注意：此段代码无法运行：没有找到该函数\n",
    "# # 导入AdaptiveLasso包\n",
    "# from sklearn.linear_model import AdaptiveLasso\n",
    "# model = AdaptiveLasso(gamma = 1)\n",
    "# model.fit(data.iloc[:, 0:6], data['y'])\n",
    "# model.coef_# 各个特征的系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda2\\lib\\site-packages\\sklearn\\linear_model\\coordinate_descent.py:491: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ -1.40843280e+03,   2.89759651e-02,   3.54485469e-02,\n",
       "        -5.22279522e-02,  -1.38570705e+06,  -4.94973680e-03])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.linear_model import Lasso\n",
    "# LASSO回归的特点是在拟合广义线性模型的同时进行变量筛选和复杂度调整。 因此，不论目标因变量是连续的，还是二元或者多元离散的，\n",
    "#都可以用LASSO回归建模然后预测。 这里的变量筛选是指不把所有的变量都放入模型中进行拟合，而是有选择的把变量放入模型从而得到更好的性能参数。\n",
    "model = Lasso(alpha=0.1)\n",
    "model.fit(data[['x1', 'x2', 'x3', 'x4','x5', 'x6']], data['y']) # data.iloc[:, 0:13]\n",
    "model.coef_ # 各个特征的系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "370573.12669564178"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.intercept_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ -1.52806468e+03,   6.28147298e-02,  -8.88362694e+05])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过Lasso得到的变量为 x1、x3、x5\n",
    "model = Lasso(alpha=0.1)\n",
    "model.fit(data[['x1', 'x3','x5']], data['y']) # data.iloc[:, 0:13]\n",
    "model.coef_ # 各个特征的系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
